﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Entity;
using PBOffice.DAL;

namespace PBOffice.BLL
{
   public class OrganManager
    {
        #region 查询所有机构信息
        /// <summary>
        /// 查询所有机构信息
        /// </summary>
        /// <returns></returns>
        public List<Organ> ShowAllOrganInfo()
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var organ = from o in myOffice.Organs
                            select o;
                return organ.ToList();
            }
        } 
        #endregion

        #region 查询机构信息（根据机构编号）
        /// <summary>
        /// 查询机构信息（根据机构编号）
        /// </summary>
        /// <param name="organNo"></param>
        /// <returns></returns>
        public Organ ShowOrganInfoByOrganNo(int organNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var organ = from o in myOffice.Organs
                            select o;
                return organ.ToList().FirstOrDefault();
            }
        }  
        #endregion

        #region 添加机构信息
        /// <summary>
        /// 添加机构信息
        /// </summary>
        /// <param name="o">机构对象</param>
        /// <returns>是否添加成功</returns>
        public bool AddOrganInfo(Organ o)
        {
            Organ organ = new Organ
            {
                OrganId = o.OrganId,     //机构名
                 OrganShortName = o.OrganShortName,    //机构简介
                OrganDirector = o.OrganDirector,            //机构负责人
                OrganPhone = o.OrganPhone,           //联系电话
                OrganMobile = o.OrganMobile,         //手机
                OrganFax = o.OrganFax                //传真
            };
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                myOffice.Organs.AddObject(organ);       //添加新的机构对象到数据库
                return myOffice.SaveChanges()>0;      //保存数据库现有数据,添加成功，返回true,否则，返回false
            }
        } 
        #endregion

        #region 修改机构信息(根据机构编号)
        /// <summary>
        /// 修改机构信息（根据机构编号）
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public bool ChangeOrganInfo(Organ o)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var organ = from org in myOffice.Organs
                            where org.OrganNo==o.OrganNo
                            select org;
                foreach (var item in organ)   //修改对象各个属性
                {
                    item.OrganId = o.OrganId;
                    item.OrganShortName = o.OrganShortName;
                    item.OrganDirector = o.OrganDirector;
                    item.OrganPhone = o.OrganPhone;
                    item.OrganMobile = o.OrganMobile;
                    item.OrganFax = o.OrganFax;
                }
                return myOffice.SaveChanges()>0;     //保存修改的数据到数据库，修改成功，返回true，否则，返回false            
            }

        } 
        #endregion

        #region 删除机构信息（根据机构编号）
        /// <summary>
        /// 删除机构信息（根据机构编号）
        /// </summary>
        /// <param name="organNo"></param>
        /// <returns></returns>
        public bool DeleteOrganInfo(int organNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var organ1 = from org in myOffice.Organs
                            where org.OrganNo == organNo
                            select org;
                organ1.First();
                Organ organ= (Organ)organ1;
                myOffice.Organs.DeleteObject(organ);    //删除指定对象
                int result = myOffice.SaveChanges();                    //保存数据库中的数据
                if (result > 0)
                    return true;            //修改成功，返回true
                else
                    return false;            //失败，返回false
            }
        } 
        #endregion
    }
}
